package top.csy8888.ch01.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.csy8888.ch01.common.MinioUtils;
import top.csy8888.ch01.vo.ResultVO;

import java.io.InputStream;

/**
 * @author CSY
 * @date 2025 - 09 - 04
 */
@RestController
public class UploadController extends BaseController {
    @PostMapping("/upload/{path}")
    public ResultVO upload(@PathVariable("path") String path,
                           @RequestParam("file") MultipartFile[] files) throws Exception {
        //判断桶是否存在，不存在创建
        if (!MinioUtils.hasBucket("myapp")) {
            MinioUtils.createBucket("myapp");
        }
        for (MultipartFile file : files) {
            //获取文件名
            String filename = file.getOriginalFilename();
            //获取文件类型
            String contentType = file.getContentType();
            //获取文件大小
            long size = file.getSize();
            //获取文件输入流
            InputStream in = file.getInputStream();
            //上传到minio的文件服务器
            MinioUtils.upload("myapp",path + "/" + filename,contentType,size,in);

        }
        return success();
    }

}
